package com.mo.design.pattern.creational.factory;

import java.util.HashMap;
import java.util.Map;

public class ReflectRegisterFactory {

    private Map<String, Class> registerProduct = new HashMap<String, Class>();

    public void registerVehicle(String vehicleId, Class vehicleClass) {
        registerProduct.put(vehicleId, vehicleClass);
    }

    public Vehicle createVehicle(String type) throws IllegalAccessException, InstantiationException {
        Class productClass = registerProduct.get(type);
        return (Vehicle) productClass.newInstance();
    }

    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
        ReflectRegisterFactory reflectRegisterFactory = new ReflectRegisterFactory();
        reflectRegisterFactory.registerVehicle("car", Car.class);
        Vehicle car = reflectRegisterFactory.createVehicle("car");
        System.out.println(car.getClass());
    }

}
